<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="订单ID">
          <el-input v-model="form.order_id" placeholder="订单ID" />
        </el-form-item>
        <el-form-item label="产品ID">
          <el-input v-model="form.product_id" placeholder="产品ID" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="uid"
            label="用户UID"
            align="center"
            width="100"
          />
          <el-table-column
            prop="order_id"
            label="订单ID"
            align="center"
            width="180"
          />
          <el-table-column
            prop="product_id"
            label="产品ID"
            align="center"
            width="200"
          />
          <el-table-column
            prop="app_id"
            label="游戏应用ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="channel_id"
            label="渠道标识"
            align="center"
            width="130"
          />
          <el-table-column
            prop="amount"
            label="金额"
            align="center"
            width="100"
          />

          <el-table-column
            prop="pay_channel"
            label="支付渠道"
            align="center"
            width="120"
          />
          <el-table-column prop="base_coin" label="币种" align="center" />
          <el-table-column
            prop="base_price_total"
            label="基准币种金额"
            align="center"
          />
          <el-table-column
            prop="base_currency"
            label="基准币种"
            align="center"
          />
          <el-table-column prop="real_price" label="实际价格" align="center" />
          <el-table-column
            prop="real_currency"
            label="实际币种"
            align="center"
          />
          <el-table-column
            prop="out_trade_no"
            label="三方订单ID"
            align="center"
          />
          <el-table-column
            prop="sandbox"
            label="Sandbox"
            align="center"
            width="130"
            :formatter="formatter"
          />
          <el-table-column
            prop="order_ext"
            label="扩展数据"
            align="center"
            width="180"
          />
          <el-table-column
            prop="client_ip"
            label="请求IP"
            align="center"
            width="180"
          />
          <el-table-column
            prop="order_status"
            label="订单状态"
            align="center"
            width="90"
            :formatter="formatterOrderStatus"
          />
          <el-table-column
            prop="notify_game_status"
            label="通知游戏状态"
            align="center"
            :formatter="formatterGameStatus"
          />
          <el-table-column
            prop="create_time"
            label="创建时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            prop="agree_time"
            label="订单成功时间"
            align="center"
            width="180"
          />
          <el-table-column
            prop="notify_agree_time"
            label="通知成功时间"
            align="center"
            width="180"
          />
          <el-table-column
            prop="update_time"
            label="更新时间"
            align="center"
            width="180"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";

import { formatterTime, formatter, exportToExcel } from "@/utils/utils";
import { orderApi } from "@/api/sdk/sdk";

const tableData = ref([]);

const orderStatus = {
  0: "初始",
  10: "订单已成功",
  20: "订单已退款",
};
const gameStatus = {
  0: "初始",
  10: "通知成功",
  "-10": "通知失败",
};

const formData = {
  order_id: "",
  product_id: "",
};
const form = reactive({ ...formData });
const loading = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_id: form.order_id,
    product_id: form.product_id,
  };
};
//格式化

const formatterOrderStatus = (row, column, cellValue, index) => {
  return orderStatus[cellValue];
};
const formatterGameStatus = (row, column, cellValue, index) => {
  return gameStatus[String(cellValue)];
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  
  try {
    const res = await orderApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(),{ export: 1 });
  const res = await orderApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    order_id: "订单ID",
    product_id: "产品ID",
    app_id: "游戏应用ID",
    channel_id: "渠道标识",
    amount: "金额",
    pay_channel: "支付渠道",
    base_coin: "币种",
    base_price_total: "基准币种金额",
    base_currency: "基准币种",
    real_price: "实际价格",
    real_currency: "实际币种",
    out_trade_no: "三方订单ID",
    sandbox: "Sandbox",
    client_ip: "请求IP",
    order_status: "订单状态",
    notify_game_status: "通知游戏状态",
    create_time: "创建时间",
    agree_time: "订单成功时间",
    notify_agree_time: "通知成功时间",
    update_time: "更新时间",
  };
  exportToExcel(res.data.results, head, "SDK-充值订单");
 
  
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
